package com.example.task1.ui.AllService.subway;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.SubwayMyCartListBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.Arrays;
import java.util.HashMap;

import retrofit2.Call;

public class SubwayCartActivity extends BaseActivity {

    private RecyclerView rvList;
    private Button btnSave;
    private SubwayCartListAdapter subwayCartListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subway_cart);
        initView();
        initData();
        setAdapter();
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SubwayCartActivity.this);
                LinearLayout linearLayout = new LinearLayout(SubwayCartActivity.this);
                EditText et_name = new EditText(SubwayCartActivity.this);
                EditText et_phone = new EditText(SubwayCartActivity.this);
                EditText et_card = new EditText(SubwayCartActivity.this);
                linearLayout.addView(et_name);
                linearLayout.addView(et_phone);
                linearLayout.addView(et_card);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                builder.setView(linearLayout);
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String idCard = et_card.getText().toString();
                        String userName = et_name.getText().toString();
                        String phonenumber = et_phone.getText().toString();
                        if (TextUtils.isEmpty(idCard) || TextUtils.isEmpty(userName) || TextUtils.isEmpty(phonenumber)) {
                            MyApplication.show("用户名和卡号和手机号不能为空");
                        } else {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("idCard", idCard);
                            map.put("userName", userName);
                            map.put("phonenumber", phonenumber);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/metro/card", Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("添加成功");
                                    initData();
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });
                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
    }

    private void setAdapter() {
        subwayCartListAdapter = new SubwayCartListAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(subwayCartListAdapter);
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/metro/card");
        Api.send(call, SubwayMyCartListBean.class, new CallBack<SubwayMyCartListBean>() {
            @Override
            public void success(SubwayMyCartListBean data) {
                subwayCartListAdapter.setData(Arrays.asList(data.getData()));
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public String getMyTitle() {
        return "乘车卡";
    }

    private void initView() {
        rvList = findViewById(R.id.rv_list);
        btnSave = findViewById(R.id.btn_save);
    }
}